package com.vibrator;

import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Vibrator;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.fueyuwhp.imnheskq151388.Airpush;
import com.google.ads.AdRequest;
import com.google.ads.AdSize;
import com.google.ads.AdView;

public class VibratorActivity extends Activity {

	private Vibrator vib;
	AdView ad;
	AdView ad1;
	public Boolean flag;
	Thread myThread;
	Thread myThreadOnBP;
	ExecutorService executor = Executors.newSingleThreadExecutor();
	Runnable runnable1;
	// 2nd way to create thread is by using Handlers,again we need to declare
	// handler within the class.
	// then we need override handleMessage function of handler class

	Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			Bundle bundle = msg.getData();
			int int1 = bundle.getInt("ramit");
			TextView myTextView = (TextView) findViewById(R.id.textView1);
			myTextView.setText("Button Pressed" + int1);
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// Below function was inherited from class Activity which inflates xml
		// file give as argument to the activity in which
		// it is called
		setContentView(R.layout.activity_vibrator);

		final RelativeLayout rl = (RelativeLayout) findViewById(R.id.RelLayout);
		AdRequest adRequest = new AdRequest();
		AdRequest adRequest1 = new AdRequest();

		// New custom thread class which is further invoking thread in main UI
		// thread using function runOnUiThread
		// so that it can change stuff in main UI.
		AirPushThread airPushThread = new AirPushThread();
		airPushThread.start();

		ad = (AdView) new AdView(VibratorActivity.this, AdSize.BANNER,
				"a151b8d3d40a56a");
		ad1 = (AdView) findViewById(R.id.adView);
		rl.addView(ad);

		ad.loadAd(adRequest);
		ad1.loadAd(adRequest1);

		// adRequest.addTestDevice(AdRequest.TEST_EMULATOR);

		Button button = (Button) findViewById(R.id.button1);
		vib = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
		button.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				TextView text = (TextView) findViewById(R.id.textView2);
				flag = true;
				text.setText("oye mein change ho gaya");
				try {
					if (vib.hasVibrator()) {
						long[] arr = { 0, 100000 };
						vib.vibrate(arr, 0);
						// vib.vibrate(10);
						text.setText("vibrator chal gaya");

					} else {
						text.setText("No vibrator");
					}
				} catch (NoSuchMethodError e1) {

				}
				runnable1 = new Runnable() {

					@Override
					public void run() {
						// another way to post changes to UI is by using
						// post method,it can be used to post changes to main
						// thread only which has privileges to update UI
						for (int k = 1; k <= 1000000; k++) {

							if (flag == false) {
								System.out.println("Thread to be stopped is"
										+ Thread.currentThread().getName());
								myThread.interrupt();
								break;

							}
							if ((k % 2) == 0)
								rl.post(new Runnable() {
									@Override
									public void run() {
										rl.setBackgroundResource(R.drawable.android_8_left);
									}
								});
							else

								rl.post(new Runnable() {
									@Override
									public void run() {
										rl.setBackgroundResource(R.drawable.android_8_right);
									}
								});

							try {
								Thread.sleep(500);
							} catch (InterruptedException e) {
							}
						}
					}
				};
				myThread = new Thread(runnable1);
				StartButtonClick startButtonClick =new StartButtonClick();
				startButtonClick.start();

				if (myThread.isAlive()) {
					System.out.println("Thread is already running");

				} else {
					System.out.println("Starting Thread again");
					System.out.println("Thread Name " + myThread.getName()
							+ "Started");
					executor.execute(myThread);

					// myThread.start();
				}

			}
		});

		Button button2 = (Button) findViewById(R.id.button2);
		button2.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Runnable runable = new Runnable() {
					@Override
					public void run() {
						// TODO Auto-generated method stub
						Message msg = handler.obtainMessage();
						Bundle bundle = new Bundle();
						Random rand = new Random();
						bundle.putInt("ramit", rand.nextInt());
						msg.setData(bundle);
						handler.sendMessage(msg);

					}

				};
				Thread myThread = new Thread(runable);
				myThread.start();

				// TODO Auto-generated method stub
				// Vibrator vib1 = (Vibrator)
				// getSystemService(Context.VIBRATOR_SERVICE);
				vib.cancel();
				TextView text = (TextView) findViewById(R.id.textView2);
				text.setText("Vibrator Stopped");
				// new changeBackground().execute("");
				flag = false;
				System.out.println(flag);

			}
		});

	}

	@Override
	public void onBackPressed() {

		Airpush airpush = new Airpush(getApplicationContext(), null);
		if (airpush != null) {
			airpush.startSmartWallAd();
		}
		super.onBackPressed();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.vibrator, menu);
		return true;
	}

	public class AirPushThread extends Thread {

		@Override
		public void run() {

			runOnUiThread(new Runnable() {
				@Override
				public void run() {
					// TODO Auto-generated method stub
					System.out.println("In airpush thread class");
					Airpush airpush = new Airpush(getApplicationContext(), null);
					airpush.startPushNotification(false);
					airpush.startIconAd();
				}
			});
			// TODO Auto-generated method stub

		}

	}

	public class StartButtonClick extends Thread {
		@Override
		public void run() {

			runOnUiThread(new Runnable() {
				@Override
				public void run() {
					// TODO Auto-generated method stub
					System.out.println("In button Click thread class");
					
				}
			});
			// TODO Auto-generated method stub

		}
	}

	// there are 2 ways to create threads in android one of them is AsyncTask
	// We always need to create a subclass which extends asyncTaks generic
	// first method invoked in doInBackground where we can't make changes to UI
	// other function in this subclass can post change to UI

	public class changeBackground extends AsyncTask<String, Void, String> {

		@Override
		protected String doInBackground(String... params) {
			// TODO Auto-generated method stub

			return "ha ha";

		}

		protected void onPostExecute(String result) {
			TextView txt = (TextView) findViewById(R.id.textView1);
			// Random rand = new Random();
			for (int i = 0; i < 5; i++) {
				Random rand = new Random();
				txt.setText(result + rand.nextDouble()); // txt.setText(result);
				try {
					Thread.sleep(5000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			// might want to change "executed" for the returned string passed
			// into onPostExecute() but that is upto you

		}

	}
}
